#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include<iostream>
#define MAX 1000
using namespace std;

//定义联系人结构体
struct Person
{
	string p_Name;  //姓名
	int p_Sex;      //性别
	int p_Age;      //年龄
	int p_phone;    //电话
};

//通讯录结构体
struct Addressbooks
{
	struct Person PersonArray[MAX];
	int p_Max;
};

//添加联系人
void addPerson(Addressbooks* abs)
{
    //判满
	if (abs->p_Max == MAX)
	{
		cout << "通讯录已满，不能添加。" << endl;
	}
	else
	{
		cout << "姓名: " << endl;
		cin >> abs->PersonArray[abs->p_Max].p_Name;
		while(1)
		{
			cout << "性别(1---男, 2---女): " << endl;
			cin >> abs->PersonArray[abs->p_Max].p_Sex;
			if (abs->PersonArray[abs->p_Max].p_Sex != 1 && abs->PersonArray[abs->p_Max].p_Sex != 2)
				cout << "输入错误，请重新输入。" << endl;
			else
				break;
		}

		cout << "年龄: " << endl;
		cin >> abs->PersonArray[abs->p_Max].p_Age;
		cout << "电话: " << endl;
		cin >> abs->PersonArray[abs->p_Max].p_phone;
		abs->p_Max++;
		cout << "添加成功。" << endl;
	}
	
	system("pause");
	system("cls");
}

//显示联系人
void showPerson(Addressbooks abs)
{
    //判空
	if (abs.p_Max == 0)
	{
		cout << "通讯录为空。" << endl;
	}
	else
	{
		for (int i = 0; i < abs.p_Max; i++)
		{
			cout << "姓名: " << abs.PersonArray[i].p_Name << "\t";
			if (abs.PersonArray[i].p_Sex == 1)
				cout << "性别: " << "男" << "\t";
			else
				cout << "性别: " << "女" << "\t";
			cout << "年龄: " << abs.PersonArray[i].p_Age << "\t";
			cout << "电话: " << abs.PersonArray[i].p_phone << endl;

		}
	}
	system("pause");
	system("cls");
}

//该联系人是否存在（内部函数）
int isExit(Addressbooks* abs, string name)
{
	if (0 == abs->p_Max)
		return -2;
	{
		for (int i = 0; i < abs->p_Max; i++)
		{
			if (name == abs->PersonArray[i].p_Name)
				return i;
		}
	}
	return -1;
}

//删除联系人
void delPerson(Addressbooks* abs)
{
	string name;
	while(1)
	{
		
		cout << "请输入需要删除联系人的姓名:" << endl;
		cin >> name;
		int i = isExit(abs, name);
		if (i == -1)
		{
			cout << "没有找到该联系人。" << endl;
		}
		else if (i == -2)
		{
			cout << "当前通讯录为空。" << endl;
		}
		else
		{
			for (int j = i; j < abs->p_Max - 1; j++)
			{
				abs->PersonArray[j] = abs->PersonArray[j + 1];
			}
			abs->p_Max--;
			cout << "删除成功。" << endl;
		}
		int cont = 0;
		while(1)
		{
			cout << "是否继续删除联系人？（1.继续， 2.不继续）" << endl;
			int flag;
			cin >> flag;
			if (flag == 2)
			{
				cont = 2;
				break;
			}
			if (flag == 1)
				break;
			else
				cout << "输入错误，请重新输入。" << endl;
		}
		if (cont == 2)
			break;

	}
	system("pause");
	system("cls");
}

//修改联系人
void changePerson(Addressbooks* abs)
{
	string name;
	while (1)
	{

		cout << "请输入需要修改联系人的姓名:" << endl;
		cin >> name;
		int i = isExit(abs, name);
		if (i == -1)
		{
			cout << "没有找到该联系人。" << endl;
		}
		else if (i == -2)
			cout << "当前通讯录为空。" << endl;
		else
		{
			cout << "姓名: " << endl;
			cin >> abs->PersonArray[i].p_Name;
			while (1)
			{
				cout << "性别(1---男, 2---女): " << endl;
				cin >> abs->PersonArray[i].p_Sex;
				if (abs->PersonArray[i].p_Sex != 1 && abs->PersonArray[i].p_Sex != 2)
					cout << "输入错误，请重新输入。" << endl;
				else
					break;
			}

			cout << "年龄: " << endl;
			cin >> abs->PersonArray[i].p_Age;
			cout << "电话: " << endl;
			cin >> abs->PersonArray[i].p_phone;
		}
		int cont = 0;
		while (1)
		{
			cout << "是否继续修改联系人？（1.继续， 2.不继续）" << endl;
			int flag;
			cin >> flag;
			if (flag == 2)
			{
				cont = 2;
				break;
			}
			if (flag == 1)
				break;
			else
				cout << "输入错误，请重新输入。" << endl;
		}
		if (cont == 2)
			break;

	}
	system("pause");
	system("cls");
}

//查找联系人
void seekPerson(Addressbooks* abs)
{
	string name;
	cout << "请输入要查找的人的姓名:" << endl;
	cin >> name;
	int i = isExit(abs, name);
	if (i == -1)
	{
		cout << "没有找到该联系人。" << endl;
	}
	else if (i == -2)
		cout << "当前通讯录为空。" << endl;
	else
	{
		cout << "姓名: " << abs->PersonArray[i].p_Name << "\t";
		if (abs->PersonArray[i].p_Sex == 1)
			cout << "性别: " << "男" << "\t";
		else
			cout << "性别: " << "女" << "\t";
		cout << "年龄: " << abs->PersonArray[i].p_Age << "\t";
		cout << "电话: " << abs->PersonArray[i].p_phone << endl;
	}
	system("pause");
	system("cls");
}

//清空联系人
void cleanPerson(Addressbooks* abs)
{
	int flag = 0;
	while (1)
	{
		cout << "是否清空联系人？（1. 是   2.否）" << endl;
		cin >> flag;
		if (flag == 2)
			break;
		else if (flag == 1)
		{
			abs->p_Max = 0;
			cout << "已清空。" << endl;
			break;
		}
		else
			cout << "输入错误，请重新输入。" << endl;
	}
	
	system("pause");
	system("cls");
}

void menu()
{
	cout << "********************************" << endl;
	cout << "*******  1 . 添加联系人 ********" << endl;
	cout << "*******  2 . 显示联系人 ********" << endl;
	cout << "*******  3 . 删除联系人 ********" << endl;
	cout << "*******  4 . 修改联系人 ********" << endl;
	cout << "*******  5 . 查找联系人 ********" << endl;
	cout << "*******  6 . 清空联系人 ********" << endl;
	cout << "*******  0 . 退      出 ********" << endl;
	cout << "********************************" << endl;

}


int main()
{
	Addressbooks abs;
	abs.p_Max = 0;
	while (1)
	{
		menu();
		cout << "请选择: " << endl;
		int input;
		cin >> input;
		switch (input)
		{
		case 0:
		{
			cout << "程序退出。" << endl;
			return 0;
		}
		case 1:
		{
			addPerson(&abs);
			break;

		}
		case 2:
		{
			showPerson(abs);
			break;
		}
		case 3:
		{
			delPerson(&abs);
			break;
		}
		case 4:
		{
			changePerson(&abs);
			break;
		}
		case 5:
		{
			seekPerson(&abs);
			break;
		}
		case 6:
		{
			cleanPerson(&abs);
			break;
		}
		default:
			cout << "输入错误，程序退出。" << endl;
			return 0;

		}
	}

	return 0;
}